# Replication archive for: Alexander Coppock, Kimberly Gross, Ethan Porter, Emily Thorson, and Thomas J. Wood, 
# "Conceptual Replication of Four Key Findings about Factual Corrections and Misinformation during the 2020 US Election: Evidence from Panel-Survey Experiments." 
# Forthcoming in the British Journal of Political Science doi:10.1017/S0007123422000631

library(plyr)
library(tidyverse)
library(magrittr)
library(emmeans)
library(estimatr)
library(broom)
library(metafor)


t1 <- "data/clean/all_panels_long.rds" %>% 
  readRDS %>% 
  rename(
    out = outcome_w1
  ) 

t2 <- t1 %>% 
  select(
    admin_id, treatment, out, lucid_pid_3, topic_short, congeniality, contains("ft_")
  ) %>% 
  gather(
    targ, ft_val, contains("_ft_"), na.rm = T
  ) %>% 
  group_by(
    topic_short, congeniality, targ
  ) %>% 
  nest

t2$mods <- t2$data %>% 
  map(
    \(i)
    lm_robust(
      ft_val ~ treatment * lucid_pid_3,
      data = i
    )
  )

t2$emm <- t2$mods %>% 
  map2(
    t2$data,
    \(i, j)
    
    emmeans(
      i,
      consec ~ treatment | lucid_pid_3,
      data = j, 
      at = list(
        lucid_pid_3 = c(
          "Democrat",
          "Republican",
          "Independent"
        )
      ),
      adjust = "none"
    )
  )

t3 <- t2 %>% 
  select(
    congeniality, topic_short, emm, targ
  ) %>% 
  pmap_dfr(
    \(congeniality, topic_short, emm, targ)
    
    
    emm$contrasts %>% 
      tidy(conf.int = T) %>% 
      mutate(
        congeniality, targ, topic_short
      )
  ) %>% 
  mutate(
    type = contrast %>% 
      str_detect("control - ") %>% 
      ifelse(
        "Misinformation effect",
        "Correction effect"
      ) %>% 
      fct_rev()
  )


t3$targ %<>% 
  str_remove(
    "w1_ft_"
  ) %>% 
  str_replace_all(
    "_", " "
  ) %>% 
  str_to_title %>% 
  str_replace_all(
    c("Who" = "WHO", "U.s." = "US")
  )


t3_1 <- t3 %>% 
  filter(
    congeniality %>% 
      str_detect("Democrats")
  ) %>%
  mutate(
    targ = targ %>% 
      fct_infreq
  )

t3_1$i2 <- str_c(
  t3_1$topic_short,
  "_",
  t3_1$targ %>% 
    as.numeric
)

t3_1$i2 %<>% 
  fct_reorder(t3_1$estimate)

t3_2 <- t3 %>% 
  filter(
    congeniality %>% 
      str_detect("Democrats", negate = T)
  ) %>%
  mutate(
    targ = targ %>% 
      fct_infreq
  )


t4 <- t3 %>% 
  group_by(
    congeniality, type, lucid_pid_3
  ) %>% 
  nest %>% 
  na.omit

t4$mm <- t4$data %>% 
  map(
    possibly(
      \(i)
      rma.uni(
        yi = estimate, sei = std.error, 
        data = i,  
        control = list(maxiter = 10000)
      ),
      NULL
    )
  )


t5 <- t4 %>% 
  select(
    -data
  ) %>% 
  pmap_dfr(
    \(lucid_pid_3, congeniality, type, mm)
    
    mm %>% 
      tidy(conf.int = T) %>% 
      select(-type) %>% 
      mutate(
        lucid_pid_3, congeniality, type
      )
  ) %>% 
  mutate(
    lab = estimate %>% 
      round(2) %>% 
      str_replace("0\\.", ".") %>% 
      str_pad(width = 3, side = "right", pad = "0") %>% 
      str_c(
        c("***", "**", "*", "") %>% 
          extract(
            p.value %>% 
              findInterval(
                c(-Inf, .001, .01, .05, Inf)
              )
          )
      )
  )

t5 %>% 
  ggplot() +
  geom_vline(
    xintercept = 0,
    size = .3,
    linetype = "dashed"
  ) +
  geom_label(
    aes(
      x =  estimate,
      y = congeniality,
      label = lab,
      color = lucid_pid_3
    ),
    size = 3,
    fill = "grey95",
    label.size = 0,
    position = position_nudge(y = .25)
  ) +
  geom_pointrange(
    aes(
      xmin = conf.low, xmax = conf.high,
      x = estimate,
      y = congeniality, 
      color = lucid_pid_3
    )
  ) +
  facet_grid(
    lucid_pid_3 ~ type 
  ) +
  labs(
    x = "Difference on 100pt feeling thermometer scale",
    y = "False claim congenial to...",
  ) 
